const mongoose = require('mongoose');

const AuditSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true
  },
  site: {
    type: mongoose.Schema.ObjectId,
    ref: 'Site',
    required: true
  },
  auditType: {
    type: String,
    enum: ['full', 'seo', 'performance', 'security', 'accessibility'],
    default: 'full'
  },
  status: {
    type: String,
    enum: ['pending', 'in-progress', 'completed', 'failed'],
    default: 'pending'
  },
  result: {
    type: Object,
    default: {}
  },
  error: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新updatedAt字段
AuditSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

module.exports = mongoose.model('Audit', AuditSchema);